package com.dnake.misc;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;

import com.dnake.v700.dxml;

import java.lang.reflect.Method;

public class SysBT {
	private static BroadcastReceiver mReceiver = null;
	private static Handler mDelayHandler = new Handler();
	private static Runnable mDelayRun = null;

	public static int mEnabled = 0;
	public static String mName = "SmartBT";

	public static void load() {
		dxml p = new dxml();
		p.load("/dnake/cfg/sys_bt.xml");
		mEnabled = p.getInt("/sys/enabled", 0);
		mName = p.getText("/sys/name", "SmartBT");
		setEnabled(mEnabled == 0 ? false : true);
	}

	public static void save() {
		dxml p = new dxml();
		p.setInt("/sys/enabled", mEnabled);
		p.setText("/sys/name", mName);
		p.save("/dnake/cfg/sys_bt.xml");
		setEnabled(mEnabled == 0 ? false : true);
	}

	public static void start(Context ctx) {
		mReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				String action = intent.getAction();
				if (action.equals(BluetoothDevice.ACTION_FOUND)) {
					//BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
					//System.err.println(device.getName() + " " + device.getAddress());
				} else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
				}
			}
		};
		ctx.registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
		ctx.registerReceiver(mReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));

		mDelayRun = new Runnable() {
			@Override
			public void run() {
				if (isEnabled()) {
					setDiscoverableTimeout(300);
					mDelayHandler.postDelayed(this, 300*1000);
				}
			}
		};

		SysBT.load();
	}

	public static Boolean isSupport() {
		if (BluetoothAdapter.getDefaultAdapter() != null)
			return true;
		return false;
	}

	public static Boolean isEnabled() {
		BluetoothAdapter b = BluetoothAdapter.getDefaultAdapter();
		if (b != null)
			return b.isEnabled();
		return false;
	}

	public static void setEnabled(Boolean enabled) {
		BluetoothAdapter b = BluetoothAdapter.getDefaultAdapter();
		if (b != null) {
			if (enabled) {
				if (!isEnabled()) {
					b.enable();
					b.setName(mName);
					b.startDiscovery();
				}
				setDiscoverableTimeout(300);
				mDelayHandler.removeCallbacks(mDelayRun);
				mDelayHandler.postDelayed(mDelayRun, 300*1000);
			} else {
				if (isEnabled())
					b.disable();
			}
		}
	}

	// timeout
	public static void setDiscoverableTimeout(int timeout) {
		BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
		try {
			Method m = BluetoothAdapter.class.getMethod("setDiscoverableTimeout", int.class);
			m.setAccessible(true);
			m.invoke(adapter, timeout);
			m = BluetoothAdapter.class.getMethod("setScanMode", int.class, int.class);
			m.setAccessible(true);
			m.invoke(adapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
